//
//  GYRefundListViewController.m
//  GoodLuck
//
//  Created by 郭为勇 on 2018/3/25.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

/** 每页最多加载多少条数据 */
static NSString *count = @"20";

#import "GYRefundListViewController.h"
#import "GYNetWorkTools.h"
#import "GYNoAddressView.h"
#import "GYRefundListTableViewCell.h"
#import "GYRefundList.h"
#import "GYRefundDetailsViewController.h"
@interface GYRefundListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 数据当前的页数 */
//@property (nonatomic, assign) NSInteger currentIndex;

/** 代表是否用更多的数据 */
//@property (nonatomic, getter=isHasmore) BOOL hasmore;

/** 表格的数据源 */
@property (nonatomic, strong) NSMutableArray *refundLists;

/** 当前选择 */
@property (nonatomic, assign) NSInteger currentSelectIndex;

/** 没有数据展示的视图 */
@property (nonatomic, strong) GYNoAddressView *noRefundListView;
@end

@implementation GYRefundListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    [self setupUI];
}

- (void)setupUI {
    
    //self.currentIndex = 1;
    //self.hasmore = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UISegmentedControl *segementControlelr = [[UISegmentedControl alloc] initWithItems:@[@"退款列表",@"退货列表"]];
    segementControlelr.frame = CGRectMake(0, 0, 200, 30);
    segementControlelr.selectedSegmentIndex = 0;
    segementControlelr.tintColor = RGBColor(0, 158, 214);
    [segementControlelr setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    [segementControlelr addTarget:self action:@selector(changedSelect:) forControlEvents:UIControlEventValueChanged];

    self.navigationItem.titleView  = segementControlelr;
    self.currentSelectIndex = segementControlelr.selectedSegmentIndex;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.refundLists removeAllObjects];
    if (self.currentSelectIndex == 0) {
        [self loadRefundLsit:@""];
    }else {
        [self loadReturnGoodsList:@""];
    }
}


- (void)changedSelect:(UISegmentedControl *)segement {
    
    if (self.currentSelectIndex != segement.selectedSegmentIndex) {
//        self.currentIndex = 1;
//        self.hasmore = YES;
        [self.refundLists removeAllObjects];
        
        switch (segement.selectedSegmentIndex) {
            case 0:
            {
                //[self loadRefundLsit:[NSString stringWithFormat:@"%zd",self.currentIndex]];
                [self loadRefundLsit:@""];
            }
                break;
                
            case 1:
            {
                //[self loadReturnGoodsList:[NSString stringWithFormat:@"%zd",self.currentIndex]];
                [self loadReturnGoodsList:@""];
            }
                break;
                
            default:
                break;
        }
    }
    self.currentSelectIndex = segement.selectedSegmentIndex;
}


- (void)loadRefundLsit:(NSString *)curpage {
//    if (self.isHasmore) {
    [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
        EasyLodingConfig *config = [EasyLodingConfig shared];
        config.showOnWindow = YES;
        
        return config;
    }];
        [GYNetWorkTools requestRefundList:curpage page:count success:^(NSDictionary *obj) {
            //NSLog(@"obj=======%@",obj);
            int code = [[obj objectForKey:@"code"] intValue];
            NSDictionary *datas = [obj objectForKey:@"datas"];
            if (code == 200) {
                [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
                NSArray *refund_list = [datas objectForKey:@"refund_list"];
                [self.refundLists addObjectsFromArray:[GYRefundList jsonRefundList:refund_list]];
                if (self.refundLists.count == 0) {
                    if (self.noRefundListView != nil) {
                        self.noRefundListView.hidden = NO;
                    }else {
                        [self getNoRefundListView];
                    }
                }else {
                    self.noRefundListView.hidden = YES;
                    [self.tableView reloadData];
                }
                [self.tableView reloadData];
            }else if (code == 400) {
                [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
                [EasyTextView showText:@"加载数据出错,请稍后尝试!"];
            }
            //NSLog(@"obj ===== %@",obj);
        } error:^(NSError *error) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"加载数据出错,请稍后尝试!"];
            NSLog(@"returnListFiled===%@",error);
        }];
//    }else {
//    [EasyTextView showText:@"没有更多数据了!"];
//        NSLog(@"没有数据加载了...");
//    }
}

- (void)loadReturnGoodsList:(NSString *)curpage {
    [EasyLodingView showLodingText:@"正在加载..."];
    [GYNetWorkTools requestReturnGoodsList:curpage page:count success:^(NSDictionary *obj) {
        //NSLog(@"goods====%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            [EasyLodingView hidenLoding];
            NSArray *return_list = [datas objectForKey:@"return_list"];
            [self.refundLists addObjectsFromArray:[GYRefundList jsonRefundList:return_list]];
            if (self.refundLists.count == 0) {
                if (self.noRefundListView != nil) {
                    self.noRefundListView.hidden = NO;
                }else {
                    [self getNoRefundListView];
                }
            }else {
                self.noRefundListView.hidden = YES;
                [self.tableView reloadData];
            }
        }else if (code == 400) {
            [EasyTextView showText:@"加载数据出错,请稍后尝试!"];
            [EasyLodingView hidenLoding];
        }
    } error:^(NSError *error) {
        [EasyTextView showText:@"加载数据出错,请稍后尝试!"];
        [EasyLodingView hidenLoding];
        NSLog(@"laodRetutnGoodsListFailed-====%@",error);
    }];
}


#pragma mark --  懒加载

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.refundLists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYRefundListTableViewCell *cell = [GYRefundListTableViewCell cellWithTableView:tableView];
    cell.refundListModel = self.refundLists[indexPath.row];
    __weak typeof(self) weakSelf = self;
    cell.refunddDetailsCallBack = ^{
        GYRefundDetailsViewController *refundListVC = [[GYRefundDetailsViewController alloc] initWithNibName:@"GYRefundDetailsViewController" bundle:nil];
        refundListVC.refundModel = weakSelf.refundLists[indexPath.row];
        [weakSelf.navigationController pushViewController:refundListVC animated:YES];
    };
    
    //自动加载数据
//    if (indexPath.row == self.refundLists.count-1) {
//        if (self.currentSelectIndex == 0) {
//            [self loadRefundLsit:[NSString stringWithFormat:@"%zd",self.currentIndex]];
//        }else {
//            [self loadReturnGoodsList:[NSString stringWithFormat:@"%zd",self.currentIndex]];
//        }
//    }
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYRefundListTableViewCell *cell = [GYRefundListTableViewCell cellWithTableView:tableView];
    
    return [cell getHeight:self.refundLists[indexPath.row]];
}


#pragma mark -- 懒加载

- (NSMutableArray *)refundLists {
    if (!_refundLists) {
        _refundLists = [NSMutableArray array];
    }
    
    return _refundLists;
}

- (void)getNoRefundListView {
    GYNoAddressView *noRefundListView = [[[NSBundle mainBundle] loadNibNamed:@"GYNoAddressView" owner:nil options:nil] firstObject];
    noRefundListView.markImageView.image = [UIImage imageNamed:@"mcc_04_w"];
    noRefundListView.headLable.text = @"您还没有退货信息";
    noRefundListView.footLable.text = @"已购订单详情可以申请退货";
    noRefundListView.addBtu.hidden = YES;
    noRefundListView.frame = self.tableView.bounds;
    
    [self.view addSubview:noRefundListView];
    self.noRefundListView = noRefundListView;
}







@end
